package com.server.common.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;

public class PPT_2_JPG_2_PDF_Util {
	
	public static final int PPT_SAVEAS_JPG = 17;
	
	public static boolean pptToPdf(File inputFile, File outputFile){
		String jpgPathStr = outputFile.getPath().replaceAll(".pdf", ".jpg");
		pptToImg(inputFile.getPath(),jpgPathStr);
		imgMerageToPdf(new File(jpgPathStr.replaceAll(".jpg", "")).listFiles(),outputFile);
		return true;
	}
	
	public static boolean pptToImg(String inputFile, String imgFile) {
		// 打开word应用程序
		ActiveXComponent app = new ActiveXComponent("PowerPoint.Application");
		// 设置word不可见，office可能有限制
		// app.setProperty("Visible", false);
		// 获取word中国所打开的文档，返回Documents对象
		Dispatch files = app.getProperty("Presentations").toDispatch();
		// 调用Documents对象中Open方法打开文档，并返回打开的文档对象Document
		Dispatch file = Dispatch.call(files, "open", inputFile, true, true, false).toDispatch();
		// 调用Document对象的SaveAs方法，将文档保存为pdf格式
		// Dispatch.call(doc, "ExportAsFixedFormat", outputFile,
		// PPT_TO_PDF);
		Dispatch.call(file, "SaveAs", imgFile, PPT_SAVEAS_JPG);
		// 关闭文档
		// Dispatch.call(file, "Close", false);
		Dispatch.call(file, "Close");
		// 关闭word应用程序
		// app.invoke("Quit", 0);
		app.invoke("Quit");
		System.out.println("PPT--->转换图片成功"+inputFile);
		return true;
	}
	
	
	public static boolean imgMerageToPdf(File[] list, File file){
		//1：对图片文件通过TreeMap以名称进行自然排序
		Map<Integer,File> mif = new TreeMap<Integer,File>();
		for(File f : list)
			mif.put(Integer.parseInt(f.getName().replaceAll("幻灯片", "").replaceAll(".jpg$", "")), f);
		
		//2：获取第一个Img的宽、高做为PDF文档标准
		ByteArrayOutputStream baos = new ByteArrayOutputStream(2048*3);
		try {
			InputStream is;
			is = new FileInputStream(mif.get(1));
			for(int len;(len=is.read())!=-1;)
				baos.write(len);
			
			baos.flush();
			Image image = Image.getInstance(baos.toByteArray());
			float width = image.width();
			float height = image.height();
			baos.close();
			
			//3:通过宽高 ，实例化PDF文档对象。
			Document document = new Document(new Rectangle(width,height));
			PdfWriter pdfWr = PdfWriter.getInstance(document, new FileOutputStream(file));
			document.open();
			
			//4：获取每一个图片文件，转为IMG对象。装载到Document对象中
			for(Entry<Integer,File> eif : mif.entrySet())
			{
				//4.1:读取到内存中
				baos = new ByteArrayOutputStream(2048*3);
				is = new FileInputStream(eif.getValue());
				for(int len;(len=is.read())!=-1;)
					baos.write(len);
				baos.flush();
				
				//4.2通过byte字节生成IMG对象
				image = Image.getInstance(baos.toByteArray());
				Image.getInstance(baos.toByteArray());
				image.setAbsolutePosition(0.0f, 0.0f);
				
				//4.3：添加到document中
				document.add(image);
				document.newPage();
				baos.close();
			}
			
			//5：释放资源
			document.close();
			pdfWr.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadElementException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("图片--->转换PDF成功"+file.getPath());
		return true;
	}
	
}
